package Chapter9;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.Assert.*;
import java.io.File;


public class HTML5VideoPlayer {
    private WebDriver driver;
    private StringBuffer verificationErros = new StringBuffer();

    @Before
    public void setUp()
    {
        driver = new ChromeDriver();
        driver.get("http://dl.dropbox.com/u/55228056/html5video.html");
    }

    @Test
    public void testHTML5VideoPlayer() throws Exception
    {
        File scrFile = null;

        WebElement videoPlayer = driver.findElement(By.id("vplayer"));

        JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;

        String source = (String) jsExecutor.executeScript("return arguments[0].currentSrc;", videoPlayer);
        long duration = (Long) jsExecutor.executeScript("return arguments[0].duration", videoPlayer);

        //verify correct video is loaded and duration
        assertEquals("http://html5demos.com/assets/dizzy.mp4", source);
        assertEquals(25, duration);

        //play the video
        jsExecutor.executeScript("return arguments[0].play()", videoPlayer);

        Thread.sleep(5000);

        //pause the video
        jsExecutor.executeScript("arguments[0].pause()", videoPlayer);
        //take a screenshot for later verification
        scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("d:\\ZabaryloMaksym\\pause_play.png"));
    }
    @After
    public void tearDown()
    {
        driver.quit();
        String verificationErrorString = verificationErros.toString();
        if (!"".equals(verificationErrorString))
        {
            fail(verificationErrorString);
        }
    }
}
